package com.wgj.android.fragment;


import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.jiaai.xui.view.widget.XTopbarView;
import com.jiaai.xui.view.widget.tablayout.CommonTabLayout;
import com.wgj.android.R;
import com.wgj.android.entity.ColourModel;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.ui.activity.ShareCardActivity;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.utils.X5WebView;
import com.wgj.android.web.WGJWebActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class TemplateFragment extends Fragment {


    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.view5)
    View view5;
    @BindView(R.id.view6)
    View view6;
    @BindView(R.id.topBar)
    XTopbarView topBar;
    private View view[];
    private CommonTabLayout table;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_template, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        topBar.setTopBarClickListener(new XTopbarView.OnTopBarClickListener() {
            @Override
            public void onBackClick() {
                getActivity().finish();
            }

            @Override
            public void onRightButClick() {//"http://192.168.1.110:8082/#/card/edit/business"
                WGJWebActivity.openHtml5(getActivity(),WGJAPI.WEB_URL+"/card/index.html#/card/edit/business?mId="+ CacheUtils.getMember().getM_id(),false);

            }
        });
        view = new View[]{view1, view2, view3, view4, view5, view6};
        XHttp.obtain().get(WGJAPI.GET_CARD_COLOUR, null, new ListHttpCallBack<ColourModel>() {
            @Override
            public void onSuccess(List<ColourModel> list) {

                for (int i = 0; i < list.size(); i++) {
                    int str = Color.parseColor(list.get(i).getModel());
                    GradientDrawable drawable = new GradientDrawable();
                    drawable.setCornerRadius(14);
                    drawable.setColor(str);
                    view[i].setBackground(drawable);
                    view[i].setTag(list.get(i).getModel());
                }
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.success("创建失败");

            }
        });
    }

    @OnClick({R.id.view1, R.id.view2, R.id.view3, R.id.view4, R.id.view5, R.id.view6})
    public void onClick(View v) {
        table.setCurrentTab(0);
        Event event = new Event();
        switch (v.getId()) {
            case R.id.view1:
                event.setColor((String) view1.getTag());
                EventBus.getDefault().postSticky(event);
                editCard((String) view1.getTag());
                break;
            case R.id.view2:
                event.setColor((String) view2.getTag());
                EventBus.getDefault().postSticky(event);
                editCard((String) view2.getTag());
                break;
            case R.id.view3:
                event.setColor((String) view3.getTag());
                EventBus.getDefault().postSticky(event);
                editCard((String) view3.getTag());
                break;
            case R.id.view4:
                event.setColor((String) view4.getTag());
                EventBus.getDefault().postSticky(event);
                editCard((String) view4.getTag());
                break;
            case R.id.view5:
                event.setColor((String) view5.getTag());
                EventBus.getDefault().postSticky(event);
                editCard((String) view5.getTag());
                break;
            case R.id.view6:
                event.setColor((String) view6.getTag());
                EventBus.getDefault().postSticky(event);
                editCard((String) view6.getTag());
                break;


        }
    }

    private void editCard(String str) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("model", str);
        //XLoadingDialog.with(getContext()).show();
        XHttp.obtain().post(WGJAPI.UPDATE_CARD, dataMap, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                XToast.success("修改成功");

            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.success("修改失败");
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        table = ((ShareCardActivity) activity).getTable();
    }


    public class Event {
        public String getColor() {
            return i;
        }

        public void setColor(String i) {
            this.i = i;
        }

        String i;

    }
}
